#!/usr/local/bin/perl

use IO::Socket;
use DBI;

my %loglevels = (	0, "debug",
			1, "info", 
			2, "warn",
			3, "error",
		); 

# set logging level to a level you would like
my $set_loglevel = 0; 

sub sclogger {
    my ($errorlevel, @msg) = @_;
    if ( $errorlevel ge $set_loglevel ) {
        ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =  localtime(time);
	$year += 1900;
        my @abbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
	my $log_time = "[$year/$abbr[$mon]/$mday - $hour:$min:$sec]";
        my $facility = "/listener:$loglevels{$errorlevel}/";
	open (sculog, '>>/var/log/scuderia/master.log');
        print sculog "$log_time $facility @msg\n";
    } 
}

sclogger(1, "Scuderia starting");

my $basedir = '/root/dmon';
my $plugins = "$basedir/plugins";

sclogger(0, "basedir: $basedir, plugins dir: $plugins");

my $port = '7801';
my $listen = '0.0.0.0';
sclogger(1, "starting scuderia on port $port, with $listen");

my $mysql_host = 'localhost';
my $mysql_pass = 'sc|_|d3r14';
my $mysql_db   = 'scuderia';
my $mysql_user = 'scuderia';

my $log_file  = '/var/log/scuderia/listener.log';

sclogger(1, "Forking to background");

#my $pid = fork();
#if (not defined $pid) {
#    die sclogger(3, "Can't fork to background! $!");
#}

# Listen on port, supporting multiple clients at the same time
$server = new IO::Socket::INET 
				Proto     => 'tcp',
				LocalPort => 7801,
				Listen	  => SOMAXCONN;
				
sclogger(1, "Creating server");
$server or die "Can't create server: $! -- $?"; 

sclogger(0, "Starting main loop");
for (;;) {

    sclogger(0, "Waiting for connections");
    my $handler = $server->accept;
    $handler or die sclogger(3, "accept: $!");
    sclogger(0, "Reading from net");
    while (<$handler>) {
	my $input = $_;
	sclogger(0, "incoming string is: $input");
	if ( $input =~ /^cmd dc/) {
	    close($handler) or die sclogger(3, "Can't close handler $!");
	} elsif ( $input =~ /^cmd shutdown/ ) {
	    sclogger(1, "Shutdown requested..");
	    close($handler) or die sclogger(3, "Can't close handler $!");
	    close($server) or die sclogger(3, "Can't close server $!");
	    exit (0);
	} elsif ( $input =~ /^str\s(.*)/ ) {
	    sclogger(1, "Calling mysql store");
	    close($handler) or die sclogger(3, "Can't close handler $!");
	    StoreMySQL($input) or die sclogger(3, "Can't call mysql function $!");
	} else {
	    sclogger(1, "Uknown command received");
	    print $handler "Uknown command\n";
	    close($handler) or die sclogger(3, "Can't close handler $!");
	}
    }
}
sclogger(1, "Closing server socket");
close($server) or die sclogger(3, "Can't close socket! $!");



sub StoreMySQL {
    @values = split(/,/, $_);
    
    my $group = substr($values[0], 4); 
    my $hostname = $values[1];
    my $check = $values[2];
    my $value = $values[3];

    my $dbh = DBI->connect("DBI:mysql:$mysql_db:$mysql_host", $mysql_user, $mysql_pass);
    my $sql_unquote = "INSERT INTO checks (`time`, `group`, `hostname`, `check`, `value`) VALUES (NOW(), '$group', '$hostname', '$check', $value)";
    $sth = $dbh->prepare($sql_unquote);
    $sth->execute or print "SQL error: $DBI::errstr\n";
}
















